package com.lmc.gobang.controller;

import com.lmc.gobang.Constants.constant;
import com.lmc.gobang.model.User;
import com.lmc.gobang.service.UserService;
import com.lmc.gobang.model.Result;
import com.lmc.gobang.utils.JwtUtil;
import com.lmc.gobang.utils.SecurityUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    @RequestMapping("/register")
    public Result registerUser(User user) {
        log.info("register user");
        if(!StringUtils.hasLength(user.getUsername()) || !StringUtils.hasLength(user.getPassword())) {
            return Result.fail("用户名或密码不能为空");
        }

        if (userService.registerUser(user)) {
            return Result.success(true);
        }else{
            return Result.fail("注册失败,请检查用户名和密码");
        }

    }

    @RequestMapping("/login")
    public Result loginUser(String username, String password, HttpServletRequest request) {
        log.info("login user");
        if(!StringUtils.hasLength(username) || !StringUtils.hasLength(password)) {
            return Result.fail("用户名或密码不能为空");
        }

        User user = userService.getUserByUsername(username);
        if(user == null ) {
            return Result.fail("没有此用户");
        }
        //密码校验
//        if(!password.equals(user.getPassword())) {
//            return Result.fail("密码错误");
//        }
        if(!SecurityUtil.checkPassword(password,user.getPassword())){
            return Result.fail("密码错误");
        }

        user.setPassword(null);
        log.info("login user:"+user);

        HttpSession session = request.getSession(true);
        session.setAttribute("user", user);
        log.info("login user2222222:"+session.getAttribute("user"));

        //密码正确，生成token
        Map<String,Object> claim = new HashMap<>();
        claim.put(constant.USER_CLAIM_ID, user.getUserId());
        claim.put(constant.USER_CLAIM_NAME, user.getUsername());
        String token = JwtUtil.getToken(claim);
        System.out.println("token:   "+token);
        return Result.success(token);

    }

    @RequestMapping("/getUserInfo")
    public Result getUserInfo(HttpServletRequest request) {
        log.info("getUserInfo");
        try {
            HttpSession session = request.getSession(false);
            User user = (User)session.getAttribute("user");
            //以上获取到的用户信息是对战前的用户信息，
            //对战后的信息需要重新从数据库中获取
            User newUser = userService.getUserByUsername(user.getUsername());
            log.info("getUserInfo:"+newUser);
            return Result.success(newUser);
        } catch (NullPointerException e) {
            return Result.fail("获取失败");
        }

    }
}
